package com.xuecheng.content.controller;

import com.xuecheng.api.content.CourseBaseControllerApi;
import com.xuecheng.api.content.model.dto.CourseBaseDTO;
import com.xuecheng.api.content.model.qo.QueryCourseModel;
import com.xuecheng.api.content.model.vo.CourseBaseVO;
import com.xuecheng.common.domain.page.PageRequestParams;
import com.xuecheng.common.domain.page.PageVO;
import com.xuecheng.common.util.SecurityUtil;
import com.xuecheng.content.convert.CourseBaseConvert;
import com.xuecheng.content.entity.CourseBase;
import org.springframework.web.bind.annotation.*;
import com.xuecheng.content.service.CourseBaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * <p>
 * 课程基本信息 前端控制器
 * </p>
 *
 * @author itcast
 */
@Slf4j
@RestController
public class CourseBaseController implements CourseBaseControllerApi {

    @Autowired
    private CourseBaseService  courseBaseService;

    @GetMapping("/course/all")
    public List<CourseBaseDTO> queryAll() {
        List<CourseBase> courseBases = courseBaseService.list();
        return CourseBaseConvert.INSTANCE.entitys2dtos(courseBases);
    }

    @GetMapping("/course/{courseBaseId}")
    public CourseBaseDTO queryById(@PathVariable("courseBaseId") Long id) {
        Long companyId = SecurityUtil.getCompanyId();
        CourseBaseDTO courseBaseDTO = courseBaseService.getCourseBaseById(id,companyId);
        return courseBaseDTO;
    }

    @PostMapping("/course/list")
    public PageVO<CourseBaseDTO> queryCourseList(PageRequestParams params, @RequestBody QueryCourseModel queryCourseModel) {
        Long companyId = SecurityUtil.getCompanyId();
        return courseBaseService.queryCourseBaseList(params,queryCourseModel,companyId);
    }

    /**
     * 课程信息新增
     * @param vo
     * @return
     */
    @PostMapping("/course")
    public CourseBaseDTO createCourseBase(@RequestBody CourseBaseVO vo) {
        //获取companyId
        Long companyId = SecurityUtil.getCompanyId();
        //将vo转成dto
        CourseBaseDTO dto = CourseBaseConvert.INSTANCE.vo2Dto(vo);
        CourseBaseDTO courseBaseDTO = courseBaseService.createCourseBase(dto,companyId);
        return courseBaseDTO;
    }

    /**
     * 修改课程信息
     * @param vo
     * @return
     */
    @PutMapping("/course")
    public CourseBaseDTO modifyCourseBase(@RequestBody CourseBaseVO vo) {
        //获取companyId
        Long companyId = SecurityUtil.getCompanyId();
        //将vo转成dto
        CourseBaseDTO dto = CourseBaseConvert.INSTANCE.vo2Dto(vo);
        CourseBaseDTO courseBaseDTO = courseBaseService.modifyCourseBase(dto,companyId);
        return courseBaseDTO;
    }

    @Override
    @DeleteMapping("/course/{courseBaseId}")
    public boolean removeCourseBase(@PathVariable Long courseBaseId) {
        //获取companyId
        Long companyId = SecurityUtil.getCompanyId();
        return courseBaseService.removeCourseBase(courseBaseId,companyId);
    }

    @Override
    @GetMapping("/course/commit/{courseBaseId}")
    public boolean commitCourseBase(@PathVariable Long courseBaseId) {
        Long companyId = SecurityUtil.getCompanyId();
        return courseBaseService.commitCourseBase(courseBaseId,companyId);
    }
}
